class Category < ActiveRecord::Base
  
    attr_accessible :name, :description, :order_position, :parent, :parent_id, :subcategories, 
		:documents, :created_by, :updated_by
	
	translates :name, :description
	
    # Validations:
	#TODO: Validate for only one language
	#validates :name, presence: true 
	validates :order_position, allow_blank: true, numericality: { only_integer: true }
    
    # Relationships:
    belongs_to :parent, class_name: 'Category'
    has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id'
    has_and_belongs_to_many :documents
	
	def children
		return subcategories, documents.with_translations(I18n.locale)
	end
	
	def subcategories
		Category.with_translations(I18n.locale).where(parent_id: self.id)
	end
	
end
